<?php

namespace app\index\controller;

class Video extends Base
{
    //视频列表
    public function all()
    {
        $videoTitle = '视频教程';
        $videos = model('Video')->order('create_time', 'desc')->paginate(5);
        $viewData = [
            'videoTitle' => $videoTitle,
            'videos' => $videos
        ];
        $this->assign($viewData);
        return view();
    }

    public function video()
    {
        $where = [];
        if (input('?video_id')) {
            $where = [
                'id' => input('video_id')
            ];
        }
        $videoInfo = model('Video')->where('id', input('id'))->find();
        $videoInfo->setInc('click');
        $videoLists = model('VideoList')->where('video_id', input('id'))->select();
        $videoList = model('VideoList')->with('comments')->where($where)->order('id')->find();
        $viewData = [
            'videoTitle' => $videoInfo['title'],
            'videoLists' => $videoLists,
            'videoList' => $videoList,
            'id' => input('id')
        ];
        $this->assign($viewData);
        return view();
    }

    public function comments()
    {
        $data = [
            'video_id' => input('video_id'),
            'member_id' => input('member_id'),
            'content' => input('content')
        ];
        $videoInfo = model('VideoList')->with('video')->where('id', input('video_id'))->find();
        $videoInfo['video']->setInc('comm_num');
        $result = model('VideoComment')->add($data);
        if ($result == 1) {
            $this->success('评论成功！');
        }else {
            $this->error($result);
        }
    }
}
